package com.demo.service.impl;

import com.demo.data.DataMap;
import com.demo.entity.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * 业务实现表
 * @date 2021/4/30
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {
    /**
     * 实际应用应该是 通过 username 查询 '用户' -> 然后查询 '用户权限' ->  '注入权限'
     * @date 2021/4/30
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //------------------------------------------start------------------------------------------
        //实际流程代码
        //实际应用是根据username进行查询User对象,然后在查权限表注入权限
//        User user = new User();
//        //模拟查询权限
//        ArrayList<Role> roles = new ArrayList<>();
//        roles.add(new Role("ROLE_1"));
//        roles.add(new Role("ROLE_2"));
//        //注入
//        user.setRoleList(roles);
//        return user;
        //------------------------------------------end------------------------------------------

        //------------------------------------------test-start------------------------------------------
        //测试部分
        User user = DataMap.getUser(username);
        if (user == null) {
            throw new RuntimeException("找不到用户！");
        }
        return user;
        //------------------------------------------test-end------------------------------------------
    }
}